﻿using System.Collections.Specialized;
using IBatisNet.Common.Utilities;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;

namespace Hr.Business
{
    public static class MapperFactory
    {
        private static ISqlMapper _mapper = null;
        private static readonly object LockedObj = new object();

        private static void InitMapper()
        {
            ConfigureHandler handler = new ConfigureHandler(Configure);
            DomSqlMapBuilder builder = new DomSqlMapBuilder();
            NameValueCollection properties = new NameValueCollection
            {
                {"connectionString", "Data Source=192.168.2.8;Initial Catalog=BuySys2;User ID=sa;Password=PK$c0311;"}
            };
            builder.Properties = properties;
            _mapper = builder.ConfigureAndWatch("Config/SqlMap.config", handler);
        }

        private static void Configure(object resource)
        {
            _mapper = null;
        }

        public static ISqlMapper Instance()
        {
            if (_mapper == null)
            {
                lock (LockedObj)
                {
                    if (_mapper == null) // double-check
                    {
                        InitMapper();
                    }
                }
            }
            return _mapper;
        }
    }
}
